
package CelestialWorkflow;

import CelestialDataAccess.*;
import CelestialDataStructures.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.List;import java.util.ArrayList;
import java.text.ParseException;

public class EmployeesControl extends ValidateControl{

    /*************************************************************************/
    /* Constructors */
    public EmployeesControl(){ }
    /*************************************************************************/
    /* Client methods */
    public Employee getEmployee(int emp_id){

        EmployeesDataAccess daEmployees = new EmployeesDataAccess();
        try{
            return daEmployees.getEmployeeByID(emp_id);
        }catch(DataAccessException ex){
            m_mapErrors.put(SYS_ERR, "System Error: " + ex.getMessage());
            return null;
        }
    }
    /*-----------------------------------------------------------------------*/
    public void getAddedToday(List<Employee> lst){

        EmployeesDataAccess daEmployees = new EmployeesDataAccess();
        try{
            daEmployees.getAddedToday(lst);
        }catch(DataAccessException ex){
            m_mapErrors.put(SYS_ERR, "System Error: " + ex.getMessage());
        }
    }
    /*-----------------------------------------------------------------------*/
    public boolean updateEmployee(Map<String, String[]> param){

        /* clear errors*/
        m_mapErrors.clear();

        /* get Employee first */
        EmployeesDataAccess daEmployees = new EmployeesDataAccess();
        Employee emp;
        try{
            int iID = Integer.parseInt(param.get("emp_id")[0]);
            emp = daEmployees.getEmployeeByID(iID);
        } catch (DataAccessException ex) {
            m_mapErrors.put(SYS_ERR, "System Error: " + ex.getMessage());
            return false;
        }
        /* validate first name and last name */
        validateNames(param.get("fst_name")[0], param.get("lst_name")[0]);

        /* validate contact */
        validateContact(param.get("contact")[0]);

        /* if errors, return */
        if(m_mapErrors.size() > 0) return false;

        /* else, proceed to populate */
        if(!populate(emp, param, false)) return false;

        /* still no errors.. proceed to update */
        return updateEmployee(emp);
    }
    /*-----------------------------------------------------------------------*/
    public boolean updateEmployee(Employee emp){

        EmployeesDataAccess daEmployees = new EmployeesDataAccess();
        try{
            daEmployees.updateEmployee(emp);
        }catch(DataAccessException ex){
            m_mapErrors.put(SYS_ERR, "System Error: " + ex.getMessage());
            return false;
        }
        return true;
    }
    /*-----------------------------------------------------------------------*/
    public boolean insertEmployee(Map<String, String[]> param){

        /* clear errors*/
        m_mapErrors.clear();

        /* validate email */
        validateEmail(param.get("email")[0]);

        /* validate first name and last name */
        validateNames(param.get("fst_name")[0], param.get("lst_name")[0]);

        /* validate DOB */
        validateDOB((param.get("dob_yr")[0]), param.get("dob_mth")[0], param.get("dob_day")[0]);

        /* validate contact */
        validateContact(param.get("contact")[0]);

        if(m_mapErrors.size() > 0) return false;

        /* if no errors from validation, proceed to populate */
        Employee emp = new Employee();
        if(!populate(emp, param, true)) return false;

        /* if no errors from population, proceed to insert */
         try{
            new EmployeesDataAccess().insertEmployee(emp);
        }catch(DataAccessException ex){
            m_mapErrors.put(SYS_ERR, "System Error: " + ex.getMessage());
            return false;
        }
        return true;
    }
    /*-----------------------------------------------------------------------*/
    public void deleteEmployee(int emp_id){

        m_mapErrors.clear();
        
        EmployeesDataAccess daEmployees = new EmployeesDataAccess();
        try{
            daEmployees.deleteEmployee(emp_id);
        } catch(DataAccessException ex){
            m_mapErrors.put(SYS_ERR, "System Error: " + ex.getMessage());
        }
    }
    /*-----------------------------------------------------------------------*/
    public void searchEmployees(String search_for, String search_field, List<Employee> lst){

        /* clear errors first */
        m_mapErrors.clear();

        EmployeesDataAccess daEmployees = new EmployeesDataAccess();
        SearchCriteria criteria = daEmployees.getSearchCriteria();

        /* set operators */
        criteria.setConnective(SearchCriteria.AND);
        criteria.setOperator(EmployeesDataAccess.FIRST_NAME, SearchCriteria.LIKE);
        criteria.setOperator(EmployeesDataAccess.LAST_NAME, SearchCriteria.LIKE);
        criteria.setOperator(EmployeesDataAccess.IS_ACTIVE, SearchCriteria.EQU);
        criteria.setOperator(EmployeesDataAccess.EMP_ID, SearchCriteria.EQU);
        criteria.setOperator(EmployeesDataAccess.EMAIL, SearchCriteria.LIKE);
        criteria.setOperator(EmployeesDataAccess.CONTACT, SearchCriteria.LIKE);

        criteria.setValue(search_field, search_for);

        try{
            daEmployees.searchEmployees(criteria, lst);
        }catch(DataAccessException ex){
            m_mapErrors.put(SYS_ERR, "System Error: " + ex.getMessage());
        }
    }

    /*************************************************************************/
    /* Helpers */
    private boolean populate(Employee emp, Map<String, String[]> param, boolean insert_mode){

        try{
            emp.setDOB(DateFormat.parse(param.get("dob_yr")[0] + "-" +
                        param.get("dob_mth")[0] + "-" +
                        param.get("dob_day")[0]));
        }catch(ParseException ex){
            m_mapErrors.put(INVALID_DOB, "Invalid date");
            return false;
        }
        try{
            emp.setEmploymentDate(DateFormat.parse(param.get("emp_yr")[0] + "-" +
                        param.get("emp_mth")[0] + "-" +
                        param.get("emp_day")[0]));
        }catch(ParseException ex){
            m_mapErrors.put(INVALID_DOB, "Invalid date");
            return false;
        }
        emp.setFirstName(param.get("fst_name")[0]);
        emp.setLastName(param.get("lst_name")[0]);
        emp.setContact(param.get("contact")[0]);
        emp.setAddressLine1(param.get("addr_l1")[0]);
        emp.setAddressLine2(param.get("addr_l2")[0]);
        emp.setPostalCode(param.get("postal")[0]);
        
        return true;
    }
    /*************************************************************************/
    /* Test */
    public static void main(String[] args){

        EmployeesControl emp_ctrl = new EmployeesControl();
        ArrayList<Employee> lstEmployees = new ArrayList<Employee>();

        emp_ctrl.searchEmployees("1000", "EmployeeID", lstEmployees);

        for(Employee emp: lstEmployees){
            System.out.println(emp.getFirstName());
        }
    }
    /*************************************************************************/
}
